package DAO;


import java.util.ArrayList;
import java.util.HashMap;

import java.util.List;

import DTO.*;

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import DTO.AccountChartDTO;
import DTO.CategoryChartDTO;

public class MyData {
	
	
	String income ;
	String expense ;
	String balance ;
	
	HashMap<String, Integer> categoty  = new HashMap<String, Integer>();
	HashMap<String, Integer> account   = new HashMap<String, Integer>();
	
	private Integer getValue(String str)
	{
		char[] buffer = new char[11];
		int c = 0;
		
		for(int i=0;i<str.length();i++)
		{
			char ch = str.charAt(i);
			if(ch >= '0' && ch <= '9')
			{
				
				buffer[c++] = ch;
			}
		}
		String s = new String(buffer);
		
		int i = Integer.parseInt(s.trim());
		
		return i;
	}
	public void readFromHtml(Document doc)
	{
		try
		{

	      Elements es = doc.getElementsByClass("c_table");
	      //Elements es = doc.getElementsByTag("table");
	      
	      
	      if(!es.isEmpty())
	      {
	    	  Element e = es.first();
	    	  Elements cTable = e.getElementsByTag("span");
	    	  if(cTable.size() >= 3)
	    	  {
	    		  income = cTable.get(0).ownText();
	    		  expense = cTable.get(1).ownText();
	    		  balance = cTable.get(2).ownText();
	    		  
	    	  }
	    	  
	      }
	      

	      es = doc.getElementsByClass("mc-module-content");
	      if(es.size()>=4)
	      {
	    	  Elements ancountsName = es.get(1).getElementsByClass("poplight");
	    	  Elements ancountsValue = es.get(1).getElementsByClass("value");
	    	  for(int i=0;i< ancountsName.size();i++)
	    	  {
	    		  account.put(ancountsName.get(i).ownText(), getValue(ancountsValue.get(i).ownText()) );
	    	  }
	    	  
	    	  Elements categoryName = es.get(2).getElementsByClass("poplight");
	    	  Elements categoryValue = es.get(2).getElementsByClass("value");
	    	  for(int i=0;i< ancountsName.size();i++)
	    	  {
	    		  categoty.put(categoryName.get(i).ownText(),getValue(categoryValue.get(i).ownText()));
	    	  }
	    	  
	      }

		}
		catch(Exception e)
		{
			
		}
	}
	public FinancialDTO readFinancial(Document doc)
	{

		FinancialDTO financial = new FinancialDTO();
	      Elements es = doc.getElementsByClass("c_table");
	      if(!es.isEmpty())
	      {
	    	  Element e = es.first();
	    	  Elements cTable = e.getElementsByTag("span");
	    	  if(cTable.size() >= 3)
	    	  {
	    		  financial.setIncome(cTable.get(0).ownText());
	    		  financial.setExpend(cTable.get(1).ownText());
	    		  financial.setBalance(cTable.get(2).ownText());
	    	  }  
	      }
	      return financial;
	}
	public List<AccountChartDTO> readAccountChart(Document doc)
	{
		List<AccountChartDTO> account = new ArrayList<AccountChartDTO>();

		Elements  es = doc.getElementsByClass("mc-module-content");
	      if(es.size()>=4)
	      {
	    	  Elements ancountsName = es.get(1).getElementsByClass("poplight");
	    	  Elements ancountsValue = es.get(1).getElementsByClass("value");
	    	  for(int i=0;i< ancountsName.size();i++)	    		  
	    	  {
	    		  AccountChartDTO temp = new AccountChartDTO();
	    		  temp.type(ancountsName.get(i).ownText());
	    		  temp.value(getValue(ancountsValue.get(i).ownText()));
	    		  
	    		  account.add(temp);
	    	  }
	      }
	      return account;
	}
	
	
	
	public List<CategoryChartDTO> readCategotyChart(Document doc)
	{
		List<CategoryChartDTO> categoty = new ArrayList<CategoryChartDTO>();

		Elements  es = doc.getElementsByClass("mc-module-content");
	      if(es.size()>=4)
	      {
	    	  Elements categoryName = es.get(2).getElementsByClass("poplight");
	    	  Elements categoryValue = es.get(2).getElementsByClass("value");
	    	  for(int i=0;i< categoryName.size();i++)
	    	  {
	    		  CategoryChartDTO temp = new CategoryChartDTO();
	    		  temp.type(categoryName.get(i).ownText());
	    		  temp.value(getValue(categoryValue.get(i).ownText()));
	    		  categoty.add(temp);
	    	  }
	      }
	      return categoty;
	}
	
	
	
	public List<HistoryDTO> readHistory(Document doc)
	{
		List<HistoryDTO> list = new ArrayList<HistoryDTO>();
		Elements  e = doc.getElementsByClass("pc-table");
		Element  e1 = e.select("tbody").first();
		
		Elements e2 = e1.select("tr");
		
		for(Element tr : e2)
		{
			HistoryDTO temp = new HistoryDTO();
			Elements td = tr.select("td");
			temp.Id (td.get(0).text());
			temp.Name(td.get(1).text());
			temp.Date(td.get(2).text());
			temp.Account(td.get(3).text());
			temp.Cateory(td.get(4).text());
			temp.Description(td.get(5).text());
			temp.Money(td.get(6).text());
			list.add(temp);
		}
		
	  //  Elements id = es.get(1).getElementsByClass("cell1");
  	   // Elements name = es.get(1).getElementsByClass("cell2");
		
		return list;
	}
	
	public IncomeDTO readIncome(Document doc)
	{
		IncomeDTO incomeDTO = new IncomeDTO();
		Elements  e = doc.getElementsByClass("c_form");
		Element e1 = e.get(7);
		Element select1 = e1.getElementsByAttributeValue("name","acc").first();
		Elements option1 = select1.children();
		for(Element temp :option1)
		{
			incomeDTO.account(temp.text());
			incomeDTO.valueacc(temp.attr("value"));
		}
		
		select1 = e1.getElementsByAttributeValue("name","bud").first();
	    option1 = select1.children();
	    for(Element temp :option1)
		{
			incomeDTO.budget(temp.text());
			incomeDTO.valuebud(temp.attr("value"));
		}
	    
	    select1 = e1.getElementsByAttributeValue("name","currency").first();
	    option1 = select1.children();
	    for(Element temp :option1)
		{
			incomeDTO.currency(temp.text());
			incomeDTO.valuecur(temp.attr("value"));
		}	
		
	  //  Elements id = es.get(1).getElementsByClass("cell1");
  	   // Elements name = es.get(1).getElementsByClass("cell2");
		
		return incomeDTO;
	}
	
	public ExpendDTO readExpend(Document doc)
	{
		ExpendDTO expendDTO = new ExpendDTO();
		Elements  e = doc.getElementsByClass("c_form");
		Element e1 = e.get(8);
		Element select1 = e1.getElementsByAttributeValue("name","cat").first();
		Elements option1 = select1.children();
		for(Element temp :option1)
		{
			expendDTO.category(temp.text());
			expendDTO.valuecate(temp.attr("value"));
		}
		
		select1 = e1.getElementsByAttributeValue("name","acc").first();
	    option1 = select1.children();
	    for(Element temp :option1)
		{
	    	expendDTO.account(temp.text());
	    	expendDTO.valueacc(temp.attr("value"));
		}
	    
	    select1 = e1.getElementsByAttributeValue("name","currency").first();
	    option1 = select1.children();
	    for(Element temp :option1)
		{
	    	expendDTO.currency(temp.text());
	    	expendDTO.valuecur(temp.attr("value"));
		}	
		
	  //  Elements id = es.get(1).getElementsByClass("cell1");
  	   // Elements name = es.get(1).getElementsByClass("cell2");
		
		return expendDTO;
	}
	
	public TransferDTO readTransfer(Document doc)
	{
		TransferDTO transferDTO = new TransferDTO();
		Elements  e = doc.getElementsByClass("c_form");
		Element e1 = e.get(3);
		Element select1 = e1.getElementsByAttributeValue("name","danhmuc").first();
		Elements option1 = select1.children();
		for(Element temp :option1)
		{
			transferDTO.account(temp.text());
			transferDTO.valueacc(temp.attr("value"));
		}
		
		select1 = e1.getElementsByAttributeValue("name","danhmuc2").first();
	    option1 = select1.children();
	    for(Element temp :option1)
		{
	    	transferDTO.accountto(temp.text());
	    	transferDTO.valueaccto(temp.attr("value"));
		}
	    
	    select1 = e1.getElementsByAttributeValue("name","currency").first();
	    option1 = select1.children();
	    for(Element temp :option1)
		{
	    	transferDTO.currency(temp.text());
	    	transferDTO.valuecur(temp.attr("value"));
		}	
		
	  //  Elements id = es.get(1).getElementsByClass("cell1");
  	   // Elements name = es.get(1).getElementsByClass("cell2");
		
		return transferDTO;
	}
	
	public String getLogin(Document doc)
	{
		Element  e = doc.getElementsByTag("title").first();
		String temp = e.text();
		return temp;
		
	}
	
	public NewAccountDTO readAddAccount(Document doc)
	{
		NewAccountDTO newAccountDTO = new NewAccountDTO();
		Elements  e = doc.getElementsByClass("c_form");
		Element e1 = e.get(1);
		Element select1 ;
		Elements option1 ;
	
	    
	    select1 = e1.getElementsByAttributeValue("name","currency").first();
	    option1 = select1.children();
	    for(Element temp :option1)
		{
	    	newAccountDTO.currency(temp.text());
	    	newAccountDTO.valuecur(temp.attr("value"));
		}	
	    
	    option1 = e1.getElementsByAttributeValue("name", "type");
	    for(Element temp :option1)
		{
	    	newAccountDTO.account(temp.text());
	    	newAccountDTO.valueacc(temp.attr("value"));
		}	
		
	  //  Elements id = es.get(1).getElementsByClass("cell1");
  	   // Elements name = es.get(1).getElementsByClass("cell2");
		
		return newAccountDTO;
	}
	
	public NewCategogyDTO readAddCategory(Document doc)
	{
		NewCategogyDTO newCategoryDTO = new NewCategogyDTO();
		Elements  e = doc.getElementsByClass("c_form");
		Element e1 = e.get(4);
		Element select1 ;
		Elements option1 ;
	
	    
	    select1 = e1.getElementsByAttributeValue("name","bud").first();
	    option1 = select1.children();
	    for(Element temp :option1)
		{
	    	newCategoryDTO.budget(temp.text());
	    	newCategoryDTO.valuebud(temp.attr("value"));
		}	
	    
	   
	
		
		return newCategoryDTO;
	}
	
	public DepositDTO readDeposit(Document doc)
	{
		DepositDTO depositDTO = new DepositDTO();
		Elements  e = doc.getElementsByClass("c_form");
		Element e1 = e.get(5);
		Element select1 ;
		Elements option1 ;
	
	    
	    select1 = e1.getElementsByAttributeValue("name","danhmuc").first();
	    option1 = select1.children();
	    for(Element temp :option1)
		{
	    	depositDTO.account(temp.text());
	    	depositDTO.valueacc(temp.attr("value"));
		}	
	    
	   
	
		
		return depositDTO;
	}
	
	public WithdrawalDTO readWithdrawal(Document doc)
	{
		WithdrawalDTO withdrawalDTO = new WithdrawalDTO();
		Elements  e = doc.getElementsByClass("c_form");
		Element e1 = e.get(6);
		Element select1 ;
		Elements option1 ;
	
	    
	    select1 = e1.getElementsByAttributeValue("name","danhmuc").first();
	    option1 = select1.children();
	    for(Element temp :option1)
		{
	    	withdrawalDTO.account(temp.text());
	    	withdrawalDTO.valueacc(temp.attr("value"));
		}	
	    
	   
	
		
		return withdrawalDTO;
	}
	
	
	
	
	
}
